package v1

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"guns-go/global"
	"guns-go/model/request"
	"guns-go/model/response"
	"guns-go/service"
	"strconv"
)

// @Tags FileInfo
// @Summary 文件预览
// @accept application/json
// @Produce application/json
// @Param data body request.SysFileInfoParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysFileInfo/preview [get]
func Preview(c *gin.Context) {

	var F request.SysFileInfoParam
	_ = c.ShouldBindQuery(&F)

	err := service.Preview(&F, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
}

// @Tags FileInfo
// @Summary 文件下载
// @accept application/json
// @Produce application/json
// @Param data body request.SysFileInfoParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysFileInfo/download [get]
func Download(c *gin.Context) {

	var F request.SysFileInfoParam
	_ = c.ShouldBindQuery(&F)

	err := service.Download(&F, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
}

// @Tags FileInfo
// @Summary 文件删除
// @accept application/json
// @Produce application/json
// @Param data body request.SysFileInfoParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysFileInfo/delete [post]
func SysFileInfoDelete(c *gin.Context) {

	var F request.SysFileInfoParam
	_ = c.ShouldBindJSON(&F)

	err := service.SysFileInfoDelete(&F)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags FileInfo
// @Summary 分页查询文件信息表
// @accept application/json
// @Produce application/json
// @Param data body request.SysFileInfoParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysFileInfo/page [post]
func SysFileInfoPage(c *gin.Context) {

	var F request.SysFileInfoParam
	_ = c.ShouldBindQuery(&F)

	err, result := service.SysFileInfoPage(&F, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(result, c)
}

// @Tags FileInfo
// @Summary 文件上传
// @accept application/json
// @Produce application/json
// @Param data body request.SysFileInfoParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysFileInfo/upload [post]
func Upload(c *gin.Context) {

	_, header, err := c.Request.FormFile("file")
	if err != nil {
		global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
		response.FailWithMessage("接收文件失败", c)
		return
	}

	err, field := service.UploadFile(header)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(strconv.Itoa(int(*field)), c)
}
